import * as Sequelize from "sequelize";
import { Application } from "egg";

interface AppAttributes {
    org_id: string;
    id: string;
    app_name: string;
    apk: string;
    // status: string;
    remark: string;
    create_user_id: string;
    modify_user_id: string;
    create_time: string;
    modify_time: string;
}

type AppInstance = Sequelize.Instance<AppAttributes> & AppAttributes;

export default (app: Application) => {
    const { STRING, CHAR, UUID, UUIDV4, DATE,NOW } = app.Sequelize;
    const attributes:SequelizeAttributes<AppAttributes> = {
        org_id: { type: UUID, primaryKey: false, allowNull: false },
        id: { type: UUID, primaryKey: true, allowNull: false, defaultValue: UUIDV4 },
        app_name: { type: STRING(50), allowNull: false },
        apk: { type: STRING(50), allowNull: false },
        /*status: { type: CHAR(3), allowNull: false, defaultValue: "000" },*/
        remark: { type: STRING(200), allowNull: false },
        create_user_id: { type: UUID, allowNull: false },
        modify_user_id: { type: UUID, allowNull: false },
        create_time: { type: DATE, allowNull: false ,defaultValue: NOW},
        modify_time: { type: DATE, allowNull: false ,defaultValue: NOW}
    };
    const model = app.model.define<AppInstance, AppAttributes>("App", attributes, {
        tableName: 't_app'
    });
    model.associate = ()=>{
        app.model.models.App.belongsTo(app.model.models.Org,{foreignKey:"org_id", targetKey:"id"})
        app.model.models.App.hasMany(app.model.models.Version,{foreignKey:"app_id",sourceKey:"id"})
    }
    return model
};
/*

(sequalize: Sequelize.Sequelize) => {
    console.log(sequalize)
    const attributes: SequelizeAttributes<AppAttributes> = {
        org_id: { type: Sequelize.UUID, primaryKey: true, allowNull: false },
        app_id: { type: Sequelize.UUID, primaryKey: true, allowNull: false, defaultValue: Sequelize.UUIDV4 },
        app_name: { type: Sequelize.STRING(50), allowNull: false },
        status: { type: Sequelize.CHAR(3), allowNull: false, defaultValue: "0" },
        description: { type: Sequelize.STRING(200), allowNull: false },
        create_user_id: { type: Sequelize.UUID, allowNull: false },
        boe_insertTime: { type: Sequelize.DATE, allowNull: false },
        boe_updateTime: { type: Sequelize.DATE, allowNull: false }
    };
    const model = sequalize.define<AppInstance, AppAttributes>("App", attributes);
    model.associate = (models)=>{
        model.belongsTo(models.Org,{foreignKey:"app_id"})
        model.hasMany(models.Version,{foreignKey:"app_id"})
    }
    return model
};*/
